import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import VuexPersist from "vuex-persist";

const vuexLocal = new VuexPersist({
    storage: window.sessionStorage,
})

export default new Vuex.Store({
    state: {
        list: [], //接收文本框的值
    },
    mutations: {
        //添加的操作
        addlist(state, name) {
            state.list.push(name);
        },
        // 改变值
        addchange(state, index) {
            console.log(state.list[index])
            let { checked } = state.list[index];
            if (checked == true) {
                state.list[index].numsb = 2;
            } else {
                state.list[index].numsb = 1;
            }
        },
        addname(state, index) {
            state.list[index].numsb = 3;
        },
        del(state, k) {
            state.list.splice(k, 1)
        },
        isedit(state, index) {
            state.list[index].isEdit = !state.list[index].isEdit
        }
    },
    actions: {

    },
    getters: {
        gnum(state) {
            let obj = {
                ondo: 0,
                done: 0,
                cancel: 0,
            };
            state.list.forEach(item => {
                switch (item.numsb) {
                    case 1:
                        obj.ondo++;
                        break;
                    case 2:
                        obj.done++;
                        break;
                    case 3:
                        obj.cancel++;
                        break;
                }
            });
            return obj;
        }
    },
    plugins: [vuexLocal.plugin],
})